// JavaScript Document
$(document).ready(function() {
	localStorage['gplusdefaultOptions'] = JSON.stringify({
			'version':'011',
			'invited': '0',
			'leftColFixed': false,
			'chatFixed': false,
			'rightColFixed': false,
			'navBarFixed': false,
			'userInputFixed': false,
			'hideSuggestions': false,
			'hideMobile': false,
			'faviconalert': true,
			'reload_g': false,
			'optlink_main': true,
			'audioalert': false,
			'audiosound': 'gong',
			'audioreminder': false,
			'audiotimer': '300000'
	});
	$('#resetButton').button({
		icons: {primary: 'ui-icon-arrowreturnthick-1-w'},
		label: 'Reset'
	}).click(function() {
		$('#resetNotification').fadeIn('slow', function() {
			localStorage['gplusoptions'] = JSON.stringify(defaultOptions);
			location.reload();
		});
	});
	if (localStorage['gplusoptions'] == null || localStorage['gplusoptions'] == undefined || localStorage['gplusoptions'] == '')
		localStorage['gplusoptions'] = localStorage['gplusdefaultOptions'];
	var defaultOptions = JSON.parse(localStorage['gplusdefaultOptions']);
	var options = JSON.parse(localStorage['gplusoptions']);
	$('#version').val(options.version);
	$('#left_sidebar_fixed').attr('checked', options.leftColFixed);
	$('#chat_fixed').attr('checked', options.chatFixed);
	$('#right_sidebar_fixed').attr('checked', options.rightColFixed);
	$('#menu_bar_fixed').attr('checked', options.navBarFixed);
	$('#user_input_fixed').attr('checked', options.userInputFixed); 
	$('#hideSuggestions').attr('checked', options.hideSuggestions);
	$('#hideMobile').attr('checked', options.hideMobile);
	$('#faviconalert').attr('checked', options.faviconalert);
	$('#reload_g').attr('checked', options.reload_g);
	$('#optlink_main').attr('checked', options.optlink_main);
	$('#audioalert').attr('checked', options.audioalert);
	$("input[name='audiosound'][value='"+options.audiosound+"']").attr("checked","checked");
	$('#audioreminder').attr('checked', options.audioreminder);
	$("input[name='audiotimer'][value='"+options.audiotimer+"']").attr("checked","checked");
	$('#resetButton').button({
		icons: {primary: 'ui-icon-arrowreturnthick-1-w'},
		label: 'Reset'
	}).click(function() {
		$('#resetNotification').fadeIn('slow', function() {
			localStorage['gplusoptions'] = JSON.stringify(defaultOptions);
			location.reload();
		});
	});
	$('#submitButton').button({
		icons: {primary: 'ui-icon-disk'},
		label: 'Save'
	}).click(function() {
		localStorage['gplusoptions'] = JSON.stringify({
			'version': $('#version').val(),
			'invited': '1',
			'leftColFixed': $('#left_sidebar_fixed').attr('checked'),
			'chatFixed': $('#chat_fixed').attr('checked'),
			'rightColFixed': $('#right_sidebar_fixed').attr('checked'),
			'navBarFixed': $('#menu_bar_fixed').attr('checked'),
			'userInputFixed': $('#user_input_fixed').attr('checked'),
			'hideSuggestions': $('#hideSuggestions').attr('checked'),
			'hideMobile': $('#hideMobile').attr('checked'),
			'faviconalert': $('#faviconalert').attr('checked'),
			'reload_g': $('#reload_g').attr('checked'),
			'optlink_main': $('#optlink_main').attr('checked'),
			'audioalert': $('#audioalert').attr('checked'),
			'audiosound': $('input[name=audiosound]:checked').val(),
			'audioreminder': $('#audioreminder').attr('checked'),
			'audiotimer': $('input[name=audiotimer]:checked').val()
		});
		$('#saveNotification').fadeIn('slow', function(){
			chrome.extension.sendRequest({reqtype: 'options'}, function(response) {
				if(response.options.reload_g){
					chrome.tabs.getAllInWindow(undefined, function(tabs) {
						for (var i = 0, tab; tab = tabs[i]; i++) {
							if (tab.url.indexOf("plus.google.com") >= 0) {
								chrome.tabs.update(tab.id, {url: tab.url});
							}
						}
					});
				}
			});
			$('#saveNotification').delay(2000).fadeOut('slow', function(){
				location.reload();
			})
		});
	});
	
	$('#content').tabs({
		disabled: [],
		selected : 0,
		fx: {height: 'toggle', opacity: 'toggle'}
	});
	
	$( "#style_settings_bars" ).accordion({
		autoHeight: false,
		navigation: true
	});

	chrome.extension.sendRequest({reqtype: 'options'}, function(response) {

		if(response.options.leftColFixed) {
			$("#chat_fixed").attr("disabled", false);
		}
		else {
			$("#chat_fixed").attr("disabled", true);
		}
		
		if(response.options.audioalert){
			if(response.options.audioreminder){
			}
			else {
				$('#2min').attr('disabled', true);
				$('#5min').attr('disabled', true);
				$('#10min').attr('disabled', true);
				$('#15min').attr('disabled', true);
			}
		}
		else {
			$('#soundgong').attr('disabled', true);
			$('#soundsonar').attr('disabled', true);
			$('#audioreminder').attr('disabled', true);
			$('#2min').attr('disabled', true);
			$('#5min').attr('disabled', true);
			$('#10min').attr('disabled', true);
			$('#15min').attr('disabled', true);
		}
		
		if(response.options.invited != "1" || response.options.invited == null){
			$( "#invite_mailer" ).dialog({
				resizable: false,
				height:320,
				width:500,
				modal: true,
				buttons: {
					"Request Invite": function() {
						localStorage['gplusoptions'] = JSON.stringify({
							'version': $('#version').val(),
							'invited': '1',
							'leftColFixed': $('#left_sidebar_fixed').attr('checked'),
							'chatFixed': $('#chat_fixed').attr('checked'),
							'rightColFixed': $('#right_sidebar_fixed').attr('checked'),
							'navBarFixed': $('#menu_bar_fixed').attr('checked'),
							'userInputFixed': $('#user_input_fixed').attr('checked'),
							'hideSuggestions': $('#hideSuggestions').attr('checked'),
							'hideMobile': $('#hideMobile').attr('checked'),
							'faviconalert': $('#faviconalert').attr('checked'),
							'reload_g': $('#reload_g').attr('checked'),
							'optlink_main': $('#optlink_main').attr('checked'),
							'audioalert': $('#audioalert').attr('checked'),
							'audiosound': $('input[name=audiosound]:checked').val(),
							'audioreminder': $('#audioreminder').attr('checked'),
							'audiotimer': $('input[name=audiotimer]:checked').val()
						});
						$("#invite_form").submit();
						$(this).dialog('close');
					}
				}
			});
		}	
		
		if(response.options.version != "011" || response.options.version == null){
			$( "#update_notification" ).dialog({
				resizable: false,
				height:280,
				width:500,
				modal: true,
				buttons: {
					"Ok": function() {
						document.getElementById("version").value = "011";
						localStorage['gplusoptions'] = JSON.stringify({
							'version': $('#version').val(),
							'invited': '1',
							'leftColFixed': $('#left_sidebar_fixed').attr('checked'),
							'chatFixed': $('#chat_fixed').attr('checked'),
							'rightColFixed': $('#right_sidebar_fixed').attr('checked'),
							'navBarFixed': $('#menu_bar_fixed').attr('checked'),
							'userInputFixed': $('#user_input_fixed').attr('checked'),
							'hideSuggestions': $('#hideSuggestions').attr('checked'),
							'hideMobile': $('#hideMobile').attr('checked'),
							'faviconalert': $('#faviconalert').attr('checked'),
							'reload_g': $('#reload_g').attr('checked'),
							'optlink_main': $('#optlink_main').attr('checked'),
							'audioalert': $('#audioalert').attr('checked'),
							'audiosound': $('input[name=audiosound]:checked').val(),
							'audioreminder': $('#audioreminder').attr('checked'),
							'audiotimer': $('input[name=audiotimer]:checked').val()
						});
						location.reload();
						$( this ).dialog( "close" );
					},
					"Close": function() {
						document.getElementById("version").value = "011";
						localStorage['gplusoptions'] = JSON.stringify({
							'version': $('#version').val(),
							'invited': '1',
							'leftColFixed': $('#left_sidebar_fixed').attr('checked'),
							'chatFixed': $('#chat_fixed').attr('checked'),
							'rightColFixed': $('#right_sidebar_fixed').attr('checked'),
							'navBarFixed': $('#menu_bar_fixed').attr('checked'),
							'userInputFixed': $('#user_input_fixed').attr('checked'),
							'hideSuggestions': $('#hideSuggestions').attr('checked'),
							'hideMobile': $('#hideMobile').attr('checked'),
							'faviconalert': $('#faviconalert').attr('checked'),
							'reload_g': $('#reload_g').attr('checked'),
							'optlink_main': $('#optlink_main').attr('checked'),
							'audioalert': $('#audioalert').attr('checked'),
							'audiosound': $('input[name=audiosound]:checked').val(),
							'audioreminder': $('#audioreminder').attr('checked'),
							'audiotimer': $('input[name=audiotimer]:checked').val()
						});
						$( this ).dialog( "close" );
					}
				}
			});
		}	
		
	});	
});